import requests
from frame_work.yaml_file import YAML
from frame_work.setting import *
from frame_work.login import AppLogin
from frame_work import menu_list
from frame_work.util import get_jid, check_tid
from frame_work.logger import Logger

logger = Logger(__name__).get_logger()


class ConfirmGoods(object):

    def __init__(self, tid, uname):
        self.uname = uname
        self.tid = tid
        self.yaml = YAML(os.path.join(base_Dir, 'data', 'data.yaml'))
        self.sign = self.get_sign()

    def get_sign(self):
        """
        app登录凭证
        :return:
        """
        app = AppLogin(self.uname)
        sign = app.appget_sign()
        return sign

    def confirm_goods(self):
        """
        模特秀确认收货
        :return:
        """
        yaml_data = self.yaml.get_data('modshow_confirm_goods')
        val = get_jid(self.tid, self.uname)
        yaml_data.get('data').update({
            'sign': self.sign,
            'jid': str(val[0].get('jid')),
            'uid': str(val[0].get('buyer_uid')),
        })
        response = requests.post(url=yaml_data.get('url'), data=yaml_data.get('data'),
                                 headers=yaml_data.get('header')).text
        if '"code":"OK"' in response:
            logger.info("模特秀确认收货成功...")
            return True
        else:
            logger.info("模特秀确认收货失败...")
            return False


def main():
    while True:
        # 显示功能菜单
        menu_list.modshow_confirm_goods_menu()
        # 选择
        choise = input("请输入您的选择: ")
        if choise in ('q', 'Q'):
            break
        try:
            tid = int(input(u'请输入活动tid: '))
            status = check_tid(tid)
            if not status:
                raise Exception("活动tid不存在try表，请核实后在重新输入")
        except Exception as e:
            logger.error("reszon => %s" % e)
        else:
            buyer_name = input(u'请输入领取资格的试客名称: ')
            if choise == '1':
                qualify = ConfirmGoods(tid, buyer_name)
                qualify.confirm_goods()
            else:
                logger.info("没有对应的选项,请按选项输入...")
                break


if __name__ == "__main__":
    main()

